package com.cnhis.cloudhealth.clinical.commons.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class ClidoctorRedis
{
    
    public static void main(String[] args)
    {
        
        testSetElements();
    }
    
    /**
     * Jedis Pool for Jedis Resource
     * 
     * @return
     */
    public static JedisPool buildJedisPool()
    {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxActive(1);
        config.setMinIdle(50);
        config.setMaxIdle(3000);
        config.setMaxWait(5000);
        JedisPool jedisPool = new JedisPool(config, "127.0.0.1", 6379);
        return jedisPool;
    }
    
    /**
     * Test List
     * 
     * @return
     */
    public static List buildTestData()
    {
        //
        List list = new ArrayList<>();
        Map map = new HashMap<>();
        map.put("name", "门诊");
        map.put("id", "1");
        Map map1 = new HashMap<>();
        map1.put("name", "住院");
        map1.put("id", "2");
        list.add(map);
        list.add(map1);
        return list;
    }
    
    /**
     * Test for
     */
    public static void testSetElements()
    {
        List testData = buildTestData();
        Jedis jedis = buildJedisPool().getResource();
        String key = "testSetElements" + new Random(1000).nextInt();
        jedis.set(key.getBytes(), ObjectTrans.serialize(testData));
        
        // 验证
        byte[] in = jedis.get(key.getBytes());
        List<Map<String,Object>> list = (List<Map<String,Object>>)ObjectTrans.deserialize(in);
        for (Map map : list)
        {
            System.out.println("testSetElements user name is:" + map.get("name"));
        }
    }
    
    /*public static void testSetEnsemble()
    {
        List testData = buildTestData();
        Jedis jedis = buildJedisPool().getResource();
        String key = "testSetEnsemble" + new Random(1000).nextInt();
        jedis.set(key.getBytes(), ListTranscoder.serialize(testData));
        
        // 验证
        byte[] in = jedis.get(key.getBytes());
        List list = (List)ListTranscoder.deserialize(in);
        for (User user : list)
        {
            System.out.println("testSetEnsemble user name is:" + user.getName());
        }
    }*/
    
}
